'''
Created on Nov 19, 2010

@author: namikata
'''

import pygame
from pygame.locals import *
from gameobjects.vector2 import Vector2
from bk.pygame.ships.game.base.GameEntity import *
from bk.pygame.ships.game.Armory import *
from pygame.color import Color

COLOR_WHITE         = Color(0xFFFFFF99)
COLOR_BLACK         = Color(0x00000099)
COLOR_RED           = Color(0xFF000099)
COLOR_GREEN         = Color(0x00FF0099)
COLOR_BLUE          = Color(0x0000FF99)
COLOR_YELLOW        = Color(0xFFFF0099)
COLOR_LIGHT_BLUE    = Color(0x0099FF99)

class UI_Player(GameEntity):
    '''
    classdocs
    '''    

    def __init__(self,objclass, objsclass, parent, is_self = False):
        '''
        Constructor
        '''
        name = parent.name+"_ui"
        GameEntity.__init__(self, parent.world, name, None, objclass, objsclass)
        
        self.parent = parent
        self.location = Vector2(*parent.location)
        self.min = 0
        self.armor = int(parent.armor)
        self.armor_max = int(parent.armor_max)
        self.energy = int(parent.armory.energy)
        self.energy_max = int(parent.armory.energy_max)
        self.cooldown = int(parent.armory.cooldown)
        self.cooldown_max = int(parent.armory.cooldown_max)
        self.charge = int(parent.armory.charge)
        self.charge_max = int(parent.armory.charge_max)
        self.name = name
        self.w2, self.h2 = parent.image.get_size()
        self.is_self = is_self
        self.font = pygame.font.SysFont("Verdana", 12)
                
    def update(self,elapsed, keyboard, mouse):       
        self.location = self.parent.location
        self.armor = self.parent.armor
        self.energy = self.parent.armory.energy
        self.cooldown = self.parent.armory.cooldown
        self.cooldown_max = self.parent.armory.cooldown_max
        self.charge = self.parent.armory.charge
        self.charge_max = self.parent.armory.charge_max
        
    def render(self, surface):
        name_text = self.font.render(self.parent.name,True,COLOR_WHITE)
        w,h = name_text.get_size()
        surface.blit(name_text,self.location + Vector2(-(w/2),-50))
        #info bars
        w,h = (102,6) #for a 100x4 bar with a 1 width border
        pp = Vector2(self.location.x-51,self.location.y+40) #initial pivot position of bars
        vs = 5 #vertical steps for multiple bars
        op = (pp.x, pp.y)
        ip = (pp.x+1,pp.y+1)

        #life box
        step = 0
        surface.fill(COLOR_WHITE,Rect(op[0],op[1]+(vs*step),w,h))
        surface.fill(COLOR_BLACK,Rect(ip[0],ip[1]+(vs*step),w-2,h-2))
        life = int((self.armor/self.armor_max)*100)
        surface.fill(COLOR_GREEN,Rect(ip[0],ip[1]+(vs*step),life,h-2))                
        
        #energy box
        step = 1
        surface.fill(COLOR_WHITE,Rect(op[0],op[1]+(vs*step),w,h))
        surface.fill(COLOR_BLACK,Rect(ip[0],ip[1]+(vs*step),w-2,h-2))
        energy = int((self.energy/self.energy_max)*100)
        surface.fill(COLOR_LIGHT_BLUE,Rect(ip[0],ip[1]+(vs*step),energy,h-2))                
        
        #cooldown box
        if(self.cooldown > 0 or self.parent.armory.shooting):
            step = 2
            cooldown = 0
            surface.fill(COLOR_WHITE,Rect(op[0],op[1]+(vs*step),w,h))
            surface.fill(COLOR_BLACK,Rect(ip[0],ip[1]+(vs*step),w-2,h-2))
            if self.parent.armory.shooting or self.cooldown_max == 0:
                cooldown = 100
            else:
                cooldown = int((self.cooldown/self.cooldown_max)*100)
            surface.fill(COLOR_RED,Rect(ip[0],ip[1]+(vs*step),cooldown,h-2))                
        #charge box
        if(self.charge > 0):
            step = 2
            surface.fill(COLOR_WHITE,Rect(op[0],op[1]+(vs*step),w,h))
            surface.fill(COLOR_BLACK,Rect(ip[0],ip[1]+(vs*step),w-2,h-2))
            charge = int((self.charge/self.charge_max)*100)
            surface.fill(COLOR_YELLOW,Rect(ip[0],ip[1]+(vs*step),charge,h-2))                

        